<?php
    /**
    * Demand view by ID
    * @author: trungdt
    * @since: Feb 2, 2011
    * @version: 1.0
    * 
    */
    if (!defined('EXPONENT')) exit('');

    /**
    * How many items per page
    * WARNING: Must implement better in module config
    */
    $max_item =15;
    // Administrator access restrict
    $lang=exponent_lang_loadFile("modules/demandmodule/actions/demand-manage.php");
    $lang2=exponent_lang_loadFile("modules/demandmodule/demand.php");

    // check permission
    $loc=null;
    $loc->mod="demandmodule";

    // get witness ID
    $id=-1;
    if (isset($_REQUEST['id']))
    {
        $id=$_REQUEST['id'];
        $temp=explode("-",$id);
        $id=intval($temp[0]);
    }

    // get page
    $page=1;
    $pages=1; 
    $start_pos=0;
    // end of pagination
    $fields_demand="{$db->prefix}demands.id, {$db->prefix}demands.name, {$db->prefix}demands.content, {$db->prefix}demands.user_id, {$db->prefix}demands.username, {$db->prefix}demands.post_date,
    {$db->prefix}demands.`status`, {$db->prefix}demands.unapprove_reason, {$db->prefix}demands.unapprove_username, {$db->prefix}demands.publish_date, {$db->prefix}demands.important_level, 1 `type`, {$db->prefix}subjects.name subject_name, sendmail, pray_count ";

    $user_id=-1;
    if (exponent_users_isLoggedIn())
    {
        $user_id = $user->id;
    }
    $where="{$db->prefix}demands.id = {$id} 
    AND ((status =2) OR ((status<2) AND (user_id = {$user_id})))";



    $data=$db->selectObjectsBySql("                
    SELECT {$fields_demand} 
    FROM 
    {$db->prefix}demands INNER JOIN {$db->prefix}subjects
    ON {$db->prefix}demands.subject_id = {$db->prefix}subjects.id
    WHERE ({$where})

    ORDER BY status ASC, post_date DESC, important_level DESC, publish_date DESC LIMIT {$start_pos},{$max_item}
    ");

    if (count($data)>0)
    {
        $show_unapprove=false;
        if (exponent_users_isLoggedIn() && 
        (exponent_permissions_check("administrate",$loc) || 
        exponent_permissions_check("approve_demand",$loc))
        )
        {
            $show_unapprove=true;
        }
        demandmodule::demand_witness_show($data, $lang, $lang2, $show_unapprove);

        $title=$lang2['demand-detail-title'];
        $page_action="demand-detail";

        $template=new template("demandmodule","_demand_manage");
        $template->assign("data",$data);
        $template->register_permissions(array('approve_demand',"administrate"),$loc);  
        $template->assign("page",$page);
        $template->assign("pages",$pages);
        $template->assign("title",$title);
        $template->assign("page_action",$page_action); 
        $template->assign("start_index",$start_pos);
        $template->assign("demand_manage",0);

        if (exponent_users_isLoggedIn())
        {
            $template->assign("logged",1);
        }
        _ab_theme_action_register_title($title." {$data[0]->name} - ".SITE_TITLE);
        $template->output();
    }
    else
    {
        echo SITE_404_HTML;
    }

?>
